<template>
  <div>
    <!-- 轮播图 -->
    <van-swipe class="my-swipe" :autoplay="3000">
      <van-swipe-item v-for="item in gallery" :key="item.id">
        <img :src="item.img_url" alt="" width="100%" style="display: block" />
      </van-swipe-item>
    </van-swipe>
    <!-- Tips组件 -->
    <Tips />
    <div class="info">
      <div class="name">{{ info.name }}</div>
      <div class="goods_brief">{{ info.goods_brief }}</div>
      <div class="retail_price">{{ info.retail_price | RMBformat }}</div>
    </div>
    <van-cell title="展示弹出层" is-link @click="isSkuShow = true" />
    <!-- 商品参数 -->
    <div class="attributer">
      <h4>商品参数</h4>
      <ul class="e1">
        <li v-for="item in attributer" :key="item.id">
          <span>{{ item.name }} </span>
          <section class="van-ellipsis">{{ item.value }}</section>
        </li>
      </ul>
    </div>
    <!-- 产品详情图片 -->
    <div class="goods_desc" v-html="goods_desc"></div>
    <!-- 常见问题 -->
    <div class="title">
      <span>常见问题</span>
    </div>
    <ul class="issue">
      <li v-for="item in issue" :key="item.id">
        <h3>{{ item.question }}</h3>
        <p>{{ item.answer }}</p>
      </li>
    </ul>
    <!-- 相关产品 -->
    <div class="title">
      <span>相关产品</span>
    </div>
    <Products :goodsList="goodsList" />

    <!-- 商品导航 -->
    <AppGoodsActive @goToCart="goToCart" :goodsCount="goodsCount" />
    <div class="footer">footer被隐藏</div>

    <!-- sku -->
    <van-sku
      ref="sku"
      v-model="isSkuShow"
      :sku="sku"
      :goods="goods"
      :hide-stock="sku.hide_stock"
    />
  </div>
</template>

<script>
import {
  GetGoodsDetailData,
  GetGoodsRelatedData,
  GetGoodscount,
  AddProductToCart,
} from "@/request/api";
import Tips from "@/components/Tips.vue";
import AppGoodsActive from "@/components/AppGoodsActive.vue";
import Products from "@/components/Products.vue";

export default {
  data() {
    return {
      // sku
      isSkuShow: false,
      sku: {
        tree: [],
        hide_stock: false,
        price: "0",
        stock_num: "0",
      },
      goods: {
        picture: "",
      },
      // 轮播图数据
      gallery: [],
      info: [],
      attributer: [],
      // 相关产品的数组 图片
      goods_desc: "",
      // 相关商品
      goodsList: [],
      // 购物车数量
      goodsCount: "",
      // 常见问题
      issue: [],
      //
      productList: [],
    };
  },
  created() {
    GetGoodsDetailData({
      id: this.$route.query.id,
    }).then((res) => {
      // console.log("购物车数据", res);
      let { gallery, info, attribute, issue, productList } = res.data;
      this.gallery = gallery;
      this.info = info;
      this.attributer = attribute;
      this.goods_desc = info.goods_desc;
      this.issue = issue;
      // sku数据
      this.goods.picture = info.list_pic_url;
      this.sku.price = info.retail_price;
      this.sku.stock_num = info.goods_number;
      this.productList = productList;
    });
    GetGoodsRelatedData({
      id: this.$route.query.id,
    }).then((res) => {
      // console.log("相关产品", res.data.goodsList);
      let { goodsList } = res.data;
      this.goodsList = goodsList;
    });

    GetGoodscount().then((res) => {
      if (res.errno == 0) {
        // console.log("购物车相关", res.data);
        this.goodsCount = res.data.cartTotal.goodsCount;
      }
      //
    });
  },
  methods: {
    goToCart() {
      if (this.isSkuShow) {
        // console.log("执行加入购物车的操作");
        // console.log(this.$refs.sku.getSkuData().selectedNum);
        // 发起购物车请求
        AddProductToCart({
          goodsId: this.$route.query.id,
          productId: this.productList[0].id,
          number: this.$refs.sku.getSkuData().selectedNum,
        }).then((res) => {
          if (res.errno == 0) {
            console.log("购物车", res.data);
            // 添加到购物车后做两件事
            // 更新购物车数量
            this.goodsCount = res.data.cartTotal.goodsCount;
            // 跳转到购物车页面
            this.$toast.success("加入购物车成功");
            setTimeout(() => {
              this.$router.push("/cart");
            }, 1500);
          }
        });
      } else {
        this.isSkuShow = true;
      }
    },
  },
  components: {
    Tips,
    AppGoodsActive,
    Products,
  },
};
</script>

<style lang="less" scoped>
.info {
  background-color: #fff;
  padding: 0.2rem 0;
  border-bottom: 1px solid #ccc;
}
.name {
  text-align: center;
  font-size: 0.2rem;
  line-height: 0.5rem;
}
.goods_brief {
  text-align: center;
  color: #999;
  font-size: 0.14rem;
  line-height: 0.3rem;
}
.retail_price {
  text-align: center;
  color: darkred;
  font-size: 0.14rem;
  line-height: 0.3rem;
}
.attributer {
  margin-top: 0.2rem;
  background: #fff;
  padding: 0.2rem 4%;
}
h4 {
  font-size: 0.16rem;
  margin-bottom: 0.2rem;
}
.e1 {
  li {
    background: #efefef;
    line-height: 0.4rem;
    height: 0.4rem;
    margin-bottom: 0.1rem;
    border-radius: 4px;
    display: flex;
    justify-content: space-between;
    span {
      width: 30%;
      text-align: right;
    }
    section {
      flex: 1;
      padding-left: 0.2rem;
    }
  }
}
/deep/.goods_desc {
  img {
    width: 100%;
    display: block;
  }
}
.title {
  width: 100%;
  background: #fff;
  height: 0.5rem;
  position: relative;
  &::before {
    content: "";
    width: 50%;
    height: 2px;
    background: #ccc;
    position: absolute;
    top: 50%;
    margin-top: -1px;
    left: 50%;
    margin-left: -25%;
  }
  span {
    width: 30%;
    position: relative;
    background: #fff;
    display: block;
    text-align: center;
    margin: 0 auto;
    height: 0.5rem;
    line-height: 0.5rem;
  }
}
.issue {
  padding: 0 4%;
  background: #fff;
  li {
    h3 {
      height: 0.3rem;
      line-height: 0.3rem;
      padding-left: 0.2rem;
      position: relative;
      font-weight: normal;
      &::before {
        content: "";
        width: 4px;
        height: 4px;
        background: darkred;
        display: inline-block;
        position: absolute;
        left: 0;
        top: 50%;
        margin-top: -2px;
      }
    }
    p {
      font-size: 0.12rem;
      line-height: 0.2rem;
      padding-left: 0.2rem;
    }
  }
}
.footer {
  display: inline-block;
  height: 0.48rem;
}
</style>